Simple Properties: StringProperty, IntegerProperty, BooleanProperty

Java Technologies - জাভাএফএক্স (JavaFx) JavaFX Properties এবং Bindings |
110
110

JavaFX Properties হল একটি শক্তিশালী বৈশিষ্ট্য যা UI উপাদানগুলির মধ্যে ডেটার পরিবর্তন স্বয়ংক্রিয়ভাবে ট্র্যাক করতে সহায়তা করে। JavaFX-এ বিভিন্ন ধরনের Property রয়েছে, যা আপনাকে UI উপাদানগুলির মান (value) পর্যবেক্ষণ এবং পরিবর্তন করার সুবিধা দেয়। এতে UI আপডেট করার জন্য আলাদা করে কোড লেখার প্রয়োজন হয় না, কারণ Property ব্যবহার করলে ডেটা পরিবর্তন হলে UI অটোমেটিকভাবে আপডেট হয়।

JavaFX-এ সাধারণত তিনটি Property ব্যবহৃত হয়:

  1. StringProperty: স্ট্রিং মানের জন্য।
  2. IntegerProperty: পূর্ণসংখ্যা মানের জন্য।
  3. BooleanProperty: বুলিয়ান মানের জন্য।

এই Property গুলির মূল সুবিধা হলো, আপনি এগুলোকে binding করতে পারেন, যাতে ডেটার পরিবর্তন স্বয়ংক্রিয়ভাবে UI বা অন্য কোনো উপাদানে প্রতিফলিত হয়।

১. StringProperty:

StringProperty একটি স্ট্রিং ধরনের মান রাখে এবং এটি একটি JavaFX Property যা স্ট্রিং মানের পরিবর্তন ট্র্যাক করতে ব্যবহৃত হয়।

উদাহরণ: StringProperty

import javafx.beans.property.StringProperty;
import javafx.beans.property.SimpleStringProperty;

public class StringPropertyExample {
    public static void main(String[] args) {
        // StringProperty তৈরি
        StringProperty name = new SimpleStringProperty("John");

        // Value Listener যোগ করা
        name.addListener((observable, oldValue, newValue) -> {
            System.out.println("Name changed from " + oldValue + " to " + newValue);
        });

        // StringProperty মান পরিবর্তন
        name.set("Jane");
        System.out.println("Updated Name: " + name.get());
    }
}

ব্যাখ্যা:

  • StringProperty name = new SimpleStringProperty("John"); — একটি StringProperty তৈরি করা হয়েছে যার প্রাথমিক মান "John".
  • name.addListener() — Listener ব্যবহার করে যখনই name এর মান পরিবর্তন হয়, তখন তা কনসোলে দেখানো হবে।
  • name.set("Jane");name এর মান "Jane" এ পরিবর্তিত হলে listener সক্রিয় হবে এবং কনসোলে পুরানো ও নতুন মান দেখাবে।

আউটপুট:

Name changed from John to Jane
Updated Name: Jane

২. IntegerProperty:

IntegerProperty একটি পূর্ণসংখ্যা ধরনের মান রাখে এবং JavaFX এ ইনটিজার মান ট্র্যাক করতে ব্যবহৃত হয়।

উদাহরণ: IntegerProperty

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;

public class IntegerPropertyExample {
    public static void main(String[] args) {
        // IntegerProperty তৈরি
        IntegerProperty age = new SimpleIntegerProperty(25);

        // Value Listener যোগ করা
        age.addListener((observable, oldValue, newValue) -> {
            System.out.println("Age changed from " + oldValue + " to " + newValue);
        });

        // IntegerProperty মান পরিবর্তন
        age.set(30);
        System.out.println("Updated Age: " + age.get());
    }
}

ব্যাখ্যা:

  • IntegerProperty age = new SimpleIntegerProperty(25); — একটি IntegerProperty তৈরি করা হয়েছে যার প্রাথমিক মান ২৫।
  • age.addListener() — Listener যোগ করা হয়েছে যাতে age এর মান পরিবর্তন হলে সেটা কনসোলে দেখা যায়।
  • age.set(30);age এর মান ৩০ তে পরিবর্তন করা হয়েছে, যা listener এর মাধ্যমে কনসোলে প্রতিফলিত হবে।

আউটপুট:

Age changed from 25 to 30
Updated Age: 30

৩. BooleanProperty:

BooleanProperty একটি বুলিয়ান (True/False) ধরনের মান রাখে এবং JavaFX এ বুলিয়ান মান ট্র্যাক করতে ব্যবহৃত হয়।

উদাহরণ: BooleanProperty

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;

public class BooleanPropertyExample {
    public static void main(String[] args) {
        // BooleanProperty তৈরি
        BooleanProperty isActive = new SimpleBooleanProperty(true);

        // Value Listener যোগ করা
        isActive.addListener((observable, oldValue, newValue) -> {
            System.out.println("Active status changed from " + oldValue + " to " + newValue);
        });

        // BooleanProperty মান পরিবর্তন
        isActive.set(false);
        System.out.println("Updated Active status: " + isActive.get());
    }
}

ব্যাখ্যা:

  • BooleanProperty isActive = new SimpleBooleanProperty(true); — একটি BooleanProperty তৈরি করা হয়েছে যার প্রাথমিক মান true
  • isActive.addListener() — Listener যোগ করা হয়েছে যাতে isActive এর মান পরিবর্তন হলে সেটা কনসোলে দেখা যায়।
  • isActive.set(false);isActive এর মান false তে পরিবর্তন করা হয়েছে, যা listener এর মাধ্যমে কনসোলে প্রতিফলিত হবে।

আউটপুট:

Active status changed from true to false
Updated Active status: false

Properties এবং Binding:

JavaFX Properties এর মাধ্যমে আপনি binding করতে পারেন, যাতে কোনো এক property এর পরিবর্তন হলে অন্য property অটোমেটিক্যালি পরিবর্তিত হয়।

উদাহরণ: Binding

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;

public class PropertyBindingExample {
    public static void main(String[] args) {
        // IntegerProperty তৈরি
        IntegerProperty x = new SimpleIntegerProperty(5);
        IntegerProperty y = new SimpleIntegerProperty();

        // x এর সাথে y কে bind করা
        y.bind(x);

        System.out.println("x = " + x.get() + ", y = " + y.get());

        // x এর মান পরিবর্তন করলে y এর মানও পরিবর্তিত হবে
        x.set(10);
        System.out.println("After x change, x = " + x.get() + ", y = " + y.get());
    }
}

ব্যাখ্যা:

  • y.bind(x); — এখানে y কে x এর সাথে bind করা হয়েছে। এর মানে হলো x এর পরিবর্তন হলে y এর মানও স্বয়ংক্রিয়ভাবে আপডেট হবে।

আউটপুট:

x = 5, y = 5
After x change, x = 10, y = 10

সারাংশ:

  • StringProperty: স্ট্রিং মানের জন্য।
  • IntegerProperty: পূর্ণসংখ্যা মানের জন্য।
  • BooleanProperty: বুলিয়ান মানের জন্য।

JavaFX Properties আপনাকে UI উপাদানগুলির মধ্যে ডেটার পরিবর্তন ট্র্যাক করতে এবং UI ও লজিকের মধ্যে সঠিক সমন্বয় রাখতে সাহায্য করে। bind() ব্যবহার করে বিভিন্ন properties কে একে অপরের সাথে সংযুক্ত করা সম্ভব, যাতে তাদের মান স্বয়ংক্রিয়ভাবে আপডেট হয়।

Content added By
Promotion